package cn.com.shopec.erp.system.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import cn.com.shopec.erp.base.vo.SupplierVo;
import cn.com.shopec.erp.system.common.ActionContext;
import cn.com.shopec.erp.system.common.FilterOpenUrl;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.vo.UserVo;

/**
 * 
 * @ClassName PrivilegeInterceptor
 * @Description 平台登录权限过滤拦截器
 * @author chengui
 * @date 2013-6-5 下午8:23:31
 */
public class PrivilegeInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!request.getRequestURI().contains(FilterOpenUrl.suppliermanager)) {
        	if (!request.getRequestURI().contains(FilterOpenUrl.toLogin) && !request.getRequestURI().contains(FilterOpenUrl.toIndex)
                    && !request.getRequestURI().contains(FilterOpenUrl.login) && !request.getRequestURI().contains(FilterOpenUrl.logout)
                    && !request.getRequestURI().contains(FilterOpenUrl.validateCode)
                    && !request.getRequestURI().contains(FilterOpenUrl.getCommodityJsonStr)
                    && !request.getRequestURI().contains(FilterOpenUrl.getBrandJsonStr)
                    && !request.getRequestURI().contains(FilterOpenUrl.getSpecificationJsonStr)
                    && !request.getRequestURI().contains(FilterOpenUrl.icInfo)
                    && !request.getRequestURI().contains(FilterOpenUrl.multiChannelJD)
                    && !request.getRequestURI().contains(FilterOpenUrl.multiChannelTB)
                    && !request.getRequestURI().contains(FilterOpenUrl.syncChannelOrder)) {
                UserVo userVo = UserUtils.getUserVo(request);
                if (null == userVo) {
                    response.sendRedirect(request.getContextPath() + FilterOpenUrl.toLogin);
                    return false;
                }
            }
        	ActionContext.setRequest(request);
        } else if (request.getRequestURI().contains(FilterOpenUrl.suppliermanager)) {
        	if (!request.getRequestURI().contains(FilterOpenUrl.toLoginSupp) && !request.getRequestURI().contains(FilterOpenUrl.toIndexSupp)
                    && !request.getRequestURI().contains(FilterOpenUrl.loginSupp) && !request.getRequestURI().contains(FilterOpenUrl.logoutSupp)
                    && !request.getRequestURI().contains(FilterOpenUrl.validateCodeSupp)) {
        		SupplierVo suppVo = UserUtils.getSuppVo(request);
        		if (null == suppVo) {
        			response.sendRedirect(request.getContextPath() + FilterOpenUrl.toLoginSupp);
                    return false;
        		}
        	}
        	ActionContext.setRequest(request);
        }
        return super.preHandle(request, response, handler);
    }

}
